package com.servlet;

import com.bean.User;
import com.service.UserService;
import com.service.UserServiceImp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(urlPatterns = "/LoginCheck")
public class LoginCheck extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //连接数据库
        User user=new User();
        user.setName(request.getParameter("uname"));
        user.setPwd(request.getParameter("upwd"));
        //调用业务层
        UserService userService=new UserServiceImp();
        boolean boo= userService.isUser(user);
        if(boo) {
            System.out.println("成功");
            //获取session
            HttpSession session=request.getSession();
            session.setAttribute("user", user);
            //采用请求转发数据
            request.getRequestDispatcher("/index.jsp").forward(request, response);
        }else{
            System.out.println("查无此人");
            //采用重定向
            response.sendRedirect(request.getContextPath()+"/login.jsp");
        }
    }
}
